package com.nny.alarm.eidt;

import com.nny.alarm.view.DialTimeView;
import com.nny.alarm.view.DigitalTimeView;
import com.uxwine.alarm.TimeCycle;

public class CycleEditor implements IAlarmEditor
{
	AlarmEditor mEditor;
	CycleDVAdpater mAdpater;
	long mlSelfCycle;
	
	public CycleEditor(AlarmEditor e)
	{
		mEditor = e;
		mlSelfCycle = 0;
	}
	
	@Override
	public void edit()
	{
		final DigitalTimeView dttv = mEditor.mDigital;
		final DialTimeView dtv = mEditor.mDial;
		dttv.update(mEditor.mAlarm);
		
		mAdpater = new CycleDVAdpater(mEditor.mCtx);
		mAdpater.setPosition(getCyclePosition(mEditor.mAlarm.cycle));
		dtv.setAdpater(mAdpater);
		mAdpater.notifyDataChanged();
	}

	@Override
	public boolean next()
	{
		mEditor.step(AlarmEditor.STEP_TIME);
		return true;
	}

	@Override
	public void onMarked(int index)
	{
		mEditor.mAlarm.cycle.set(getCycleByPosition(index));
		mEditor.mDigital.update(mEditor.mAlarm);
	}

	@Override
	public void onMarkChange(int index)
	{
		
	}
	
	public static int getCyclePosition(TimeCycle c)
	{
		if(c.isOnce())
			return 0;
		long l = c.getCycle();
		if(l == TimeCycle.MASK_EVERYDAY)
			return 1;
		if(l == TimeCycle.MASK_WEEKENDS)
			return 2;
		if(l == TimeCycle.MASK_WEEKDAYS)
			return 3;
		if(l == TimeCycle.MASK_EVERYMONTH)
			return 4;
		if(l == TimeCycle.MASK_EVERYYEAR)
			return 5;
		return 6;
	}
	
	public long getCycleByPosition(int pos)
	{
		switch(pos)
		{
		case 0:
			return 0;
		case 1:
			return TimeCycle.MASK_EVERYDAY;
		case 2:
			return TimeCycle.MASK_WEEKENDS;
		case 3:
			return TimeCycle.MASK_WEEKDAYS;
		case 4:
			return TimeCycle.MASK_EVERYMONTH;
		case 5:
			return TimeCycle.MASK_EVERYYEAR;
		default:
			return mlSelfCycle;
		}
	}

	@Override
	public void over()
	{
		// TODO Auto-generated method stub		
	}
}
